package com.thekernel.youdaoyun.mapper;

import com.thekernel.youdaoyun.bean.Label;
import com.thekernel.youdaoyun.bean.Note;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: thekernel
 * @Date: 2020/3/26 16:29
 * @Version: 1.0
 * @Description:
 */
@Component
@Mapper
public interface LabelMapper {

    @Insert("Insert INTO `label`(`label`, `user_id`) VALUES(#{label}, #{userId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(Label label);

    @Select("SELECT * FROM `label` WHERE `user_id`=#{userId} AND `status`=1")
    @Results(
            id = "labels",
            value = {
                    @Result(property = "id", column = "id", javaType = Long.class),
                    @Result(property = "label", column = "label", javaType = String.class),
                    @Result(property = "status", column = "status", javaType = Boolean.class),
                    @Result(property = "createTime", column = "create_time", javaType = Date.class)
            }
    )
    Label[] queryLabelsByUserId(long userId);

    @Select("SELECT * FROM `label` WHERE id IN (SELECT `label_id` FROM `belong_label` WHERE `note_id`=#{noteId} AND `status`=1) AND `status`=1")
    @Results(
            id = "labels_by_note",
            value = {
                    @Result(property = "id", column = "id", javaType = Long.class),
                    @Result(property = "label", column = "label", javaType = String.class),
                    @Result(property = "status", column = "status", javaType = Boolean.class),
                    @Result(property = "createTime", column = "create_time", javaType = Date.class)
            }
    )
    Label[] queryLabelsByNoteId(int noteId);

    @Select("SELECT * FROM `note` WHERE `note_id` IN (SELECT `note_id` FROM `belong_label` WHERE `label_id`=#{labelId} AND `status`=1) AND `status`=1")
    @Results(
            id = "label_notes",
            value = {
                    @Result(property = "noteId", column = "note_id", javaType = Long.class),
                    @Result(property = "noteTitle", column = "note_title", javaType = String.class),
                    @Result(property = "noteContent", column = "note_content", javaType = String.class),
                    @Result(property = "noteUser", column = "note_user", javaType = Long.class),
                    @Result(property = "category", column = "category", javaType = Integer.class),
                    @Result(property = "status", column = "status", javaType = Boolean.class),
                    @Result(property = "createTime", column = "create_time", javaType = Date.class)
            }
    )
    Note[] queryNotesByLabelId(int labelId);

}
